package com.lobo.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Blog;
import com.lobo.blog.mappers.AdminBlogMapper;
import com.lobo.blog.service.AdminBlogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/10/4
 * @description:
 */
@Service
public class AdminBlogServiceImpl implements AdminBlogService {

    @Resource
    AdminBlogMapper adminBlogMapper;

    private static final int pageSize = 10;

    @Override
    public Blog getBlogById(int blogId) {
        Blog blog = adminBlogMapper.getBlogById(blogId);
        if (blog != null) {
            return blog;
        }
        return null;
    }

    @Override
    public PageInfo<Blog> getBlogs(int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogs = adminBlogMapper.getAllBlogByList();
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogs);
        return blogPageInfo;
    }

    @Override
    public int saveBlog(Blog blog) {
        //如果有ID就更新
        if (blog.getId() != null) {
            blog.setUpdateTime(new Date());
            String[] tagsIdArr = blog.getTags().split(",");
            adminBlogMapper.saveTag(blog.getId(), tagsIdArr);
            validateNull(blog);
            return adminBlogMapper.updateBlogById(blog.getId(), blog);
        } else {
            //否则,保存,添加创建和修改时间
            validateNull(blog);
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(1);
            blog.setIsDeleted(0);
            int result = adminBlogMapper.saveBlog(blog);
            Blog BlogInfo = adminBlogMapper.findSameTitleBlog(blog.getTitle());
            String[] tagsIdArr = blog.getTags().split(",");
            adminBlogMapper.saveTag(BlogInfo.getId(), tagsIdArr);
            return result;
        }
    }

    private void validateNull(Blog blog) {
        if (blog.getFlag() == null) {
            blog.setFlag("原创");
        }
        if (blog.getCommentable() == null) {
            blog.setCommentable(false);
        }
        if (blog.getRecommend() == null) {
            blog.setRecommend(false);
        }
        if (blog.getAppreciation() == null) {
            blog.setAppreciation(false);
        }
        if (blog.getPublished() == null) {
            blog.setPublished(false);
        }
        if (blog.getShareStatement() == null) {
            blog.setShareStatement(false);
        }
    }

    @Override
    public int updateBlog(int blogId, Blog blog) {
        //查找是否有这个Blog存在
        Blog mapperBlog = adminBlogMapper.getBlogById(blogId);
        if (mapperBlog != null) {
            adminBlogMapper.deleteBlogTags(blogId);
            adminBlogMapper.saveTag(blogId, blog.getTags().split(","));
            return adminBlogMapper.updateBlogById(blogId, blog);
        } else {
            //不存在，添加？还是返回错误？
            return 0;
        }
    }

    @Override
    public int deleteBlogById(int blogId) {
        Blog mapperBlog = adminBlogMapper.getBlogById(blogId);
        if (mapperBlog != null) {
            adminBlogMapper.deleteBlogTags(blogId);
            return adminBlogMapper.deleteBlog(blogId);
        } else {
            return 0;
        }
    }

    @Override
    public PageInfo<Blog> getBlogsByCondition(int pageNum, Blog blog) {
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogs = adminBlogMapper.getAllBlogByCondition(blog);
        PageInfo<Blog> blogPageInfo = new PageInfo<>(blogs);
        return blogPageInfo;
    }
}
